package com.xy.modular.Scheduled;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xy.framework.util.HttpUtils;
import com.xy.modular.haha.model.HahaHaha;
import com.xy.modular.haha.service.IHahaHahaService;

@Component
@Configuration // 1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class HaHa {

	@Autowired
	IHahaHahaService hahaHahaService;

	// 3.添加定时任务
	//@Scheduled(cron = "0/60 * * * * ?")
	// 或直接指定时间间隔，例如：5秒
	// @Scheduled(fixedRate=5000)
	private void configureTasks() {
		System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
		try {
			// System.out.println(HttpUtils.doPost("http://127.0.0.1:99/basics/rest/login",
			// "utf-8"));
			List<NameValuePair> urlParameters = new ArrayList<>();
			urlParameters.add(new BasicNameValuePair("pageNum", "1"));
			urlParameters.add(new BasicNameValuePair("numPerPage", "100"));
			urlParameters.add(new BasicNameValuePair("startCity", "郑州"));
			urlParameters.add(new BasicNameValuePair("endCity", "新密"));
			urlParameters.add(new BasicNameValuePair("seatNum", "1"));
			urlParameters.add(new BasicNameValuePair("goTime", "null"));
			urlParameters.add(new BasicNameValuePair("lastId", "null"));
			String str = HttpUtils.doPost("http://hh.hnxzkj.cn/WebApp/Home/GetAssList", "utf-8",urlParameters);
			JSONObject jo = JSONObject.parseObject(str);
			JSONArray array = new JSONArray();
			List l=new ArrayList<>();
			l=(List) array.toJSON(jo.get("DataSource"));
			for (int i = 0; i < l.size(); i++) {
				HahaHaha entity=new HahaHaha();
				//System.out.println(l.get(i));
				JSONObject jo3 = JSONObject.parseObject(l.get(i).toString());
				//System.out.println(jo3.get("Remark"));
				entity.setBadnum(jo3.get("BadNum").toString());
				entity.setBustype(jo3.get("BusType").toString());
				entity.setCardowner(jo3.get("CardOwner").toString());
				entity.setCash(jo3.get("Cash").toString());
				entity.setGoodnum(jo3.get("GoodNum").toString());
				entity.setGotime(jo3.get("GoTime").toString());
				entity.setId(jo3.get("Remark").toString());
				entity.setLine(filterEmoji(jo3.get("Line").toString()));
				entity.setSex(jo3.get("Sex").toString());
				entity.setUserid(jo3.get("UserId").toString());
				entity.setSeat(jo3.get("Seat").toString());
				entity.setRemark(jo3.get("Remark").toString());
				entity.setIsrealdriver(jo3.get("IsRealDriver").toString());
				entity.setIsrealname(jo3.get("IsRealName").toString());
				entity.setId(jo3.get("Id").toString());
				if(hahaHahaService.getById(entity.getId())!=null){
					hahaHahaService.update(entity);
				}else{
					hahaHahaService.save(entity);
				}
				
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
	}
	
	public static String filterEmoji(String source) {
		if (source != null) {
			Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
					Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
			Matcher emojiMatcher = emoji.matcher(source);
			if (emojiMatcher.find()) {
				source = emojiMatcher.replaceAll("*");
				return source;
			}
			return source;
		}
		return source;
	}
}
